<?php
/**
 * 配置表控制器
 */

namespace app\admin\controller;

use think\Request;
use app\common\model\ShopConfig;
use app\common\model\Goods;
use app\common\server\{ConfigServer};

use app\common\validate\ShopConfigValidate;

class ShopConfigController extends Controller
{
    //政策协议
    public function policy(Request $request, ShopConfig $model, ShopConfigValidate $validate)
    {
        if ($request->isPost()) {
            $param           = $request->param();
            ShopConfig::set('policy', 'protect', $param['protect']);
            ShopConfig::set('policy', 'empower', $param['empower']);
            $url = URL_RELOAD;
            return success('添加成功',$url);
        }
        $config = [
            'protect' => ShopConfig::get('policy', 'protect'),
            'empower' => ShopConfig::get('policy', 'empower'),
        ];
        $this->assign([
            'config'  => $config,
        ]);
        return $this->fetch();
    }

    //网站设置
    public function website(Request $request, ShopConfig $model, ShopConfigValidate $validate)
    {
        if ($request->isPost()) {
            $param           = $request->param();
            ShopConfig::set('website', 'img_url', $param['img_url']);
            ShopConfig::set('website', 'tel', $param['tel']);
            ShopConfig::set('website', 'corporate_name', $param['corporate_name']);
            ShopConfig::set('website', 'functional_module', $param['functional_module']);
            ShopConfig::set('website', 'corporate_scale', $param['corporate_scale']);
            ShopConfig::set('website', 'qq_map_key', $param['qq_map_key']);
            $url = URL_RELOAD;
            return success('添加成功',$url);
        }
        $config = [
            'img_url' => ShopConfig::get('website', 'img_url'),
            'tel' => ShopConfig::get('website', 'tel'),
            'corporate_name' => ShopConfig::get('website', 'corporate_name'),
            'functional_module' => ShopConfig::get('website', 'functional_module'),
            'corporate_scale' => ShopConfig::get('website', 'corporate_scale'),
            'qq_map_key' => ShopConfig::get('website', 'qq_map_key')
        ];
        $this->assign([
            'config'  => $config,
        ]);
        return $this->fetch();
    }

    //分享设置
    public function share(Request $request, ShopConfig $model, ShopConfigValidate $validate)
    {
        if ($request->isPost()) {
            $param           = $request->param();
            ShopConfig::set('website', 'user_share_img', $param['user_share_img']);
            ShopConfig::set('website', 'share_name', $param['share_name']);
            ShopConfig::set('website', 'share_content', $param['share_content']);
            ShopConfig::set('website', 'share_img', $param['share_img']);
            $url = URL_RELOAD;
            return success('添加成功',$url);
        }
        $config = [
            'user_share_img' => ShopConfig::get('website', 'user_share_img'),
            'share_name' => ShopConfig::get('website', 'share_name'),
            'share_content' => ShopConfig::get('website', 'share_content'),
            'share_img' => ShopConfig::get('website', 'share_img'),
        ];
        $this->assign([
            'config'  => $config,
        ]);
        return $this->fetch();
    }


    //微信设置
    public function wechat(Request $request, ShopConfig $model, ShopConfigValidate $validate)
    {
        if ($request->isPost()) {
            $param           = $request->param();
            ShopConfig::set('website', 'APPID', $param['APPID']);
            ShopConfig::set('website', 'APPSECRET', $param['APPSECRET']);
            ShopConfig::set('website', 'MCHID', $param['MCHID']);
            ShopConfig::set('website', 'WXPAYKEY', $param['WXPAYKEY']);
            ShopConfig::set('website', 'notify_url', $param['notify_url']);
            $url = URL_RELOAD;
            return success('添加成功',$url);
        }
        $config = [
            'APPID' => ShopConfig::get('website', 'APPID'),
            'APPSECRET' => ShopConfig::get('website', 'APPSECRET'),
            'MCHID' => ShopConfig::get('website', 'MCHID'),
            'WXPAYKEY' => ShopConfig::get('website', 'WXPAYKEY'),
            'notify_url' => ShopConfig::get('website', 'notify_url'),
        ];
        $this->assign([
            'config'  => $config,
        ]);
        return $this->fetch();
    }

    //分销设置
    public function distribution(Request $request, ShopConfig $model, ShopConfigValidate $validate)
    {
        if ($request->isPost()) {
            $param           = $request->param();
            ShopConfig::set('distribution', 'is_open', $param['is_open']);
            ShopConfig::set('distribution', 'level', $param['level']);
            ShopConfig::set('distribution', 'first_ratio', $param['first_ratio']);
            ShopConfig::set('distribution', 'second_ratio', $param['second_ratio']);
            ShopConfig::set('distribution', 'settlement_days', $param['settlement_days']);
            $url = URL_RELOAD;
            return success('添加成功',$url);
        }
        $config = [
            'is_open' => ShopConfig::get('distribution', 'is_open'),
            'level' => ShopConfig::get('distribution', 'level'),
            'first_ratio' => ShopConfig::get('distribution', 'first_ratio'),
            'second_ratio' => ShopConfig::get('distribution', 'second_ratio'),
            'settlement_days' => ShopConfig::get('distribution', 'settlement_days')
        ];
        $this->assign([
            'config'  => $config,
        ]);
        return $this->fetch();
    }


    //交易设置
    public function trading(Request $request, ShopConfig $model, ShopConfigValidate $validate)
    {
        if ($request->isPost()) {
            $post           = $request->param();
            ConfigServer::set('trading', 'order_cancel', $post['order_cancel']);
            ConfigServer::set('trading', 'customer_cancel_limit', $post['customer_cancel_limit']);
            ConfigServer::set('trading', 'order_finish', $post['order_finish']);
            ConfigServer::set('after_sale', 'refund_days', $post['refund_days']);
            ConfigServer::set('shop', 'contact', $post['contact']);//店铺联系人
            ConfigServer::set('shop', 'mobile', $post['mobile']);//店铺联系手机号
            ConfigServer::set('shop', 'province_id', $post['province_id']);//店铺省份id
            ConfigServer::set('shop', 'city_id', $post['city_id']);//店铺市id
            ConfigServer::set('shop', 'district_id', $post['district_id']);//店铺地区id
            ConfigServer::set('shop', 'address', $post['address']);//店铺详细地址
            $url = URL_RELOAD;
            return success('添加成功',$url);
        }
        $config = [
            'order_cancel' => ConfigServer::get('trading', 'order_cancel', 30),//未付款订单多久时间后自动关闭
            'customer_cancel_limit' => ConfigServer::get('trading', 'customer_cancel_limit', 0),//已支付订单多长时间内允许客户主动取消订单
            'order_finish' => ConfigServer::get('trading', 'order_finish', 7),//已发货订单多久时间后自动收货完成订单
            'refund_days'  => ConfigServer::get('after_sale', 'refund_days', 7),//已完成订单多久时间内允许售后退款
            'contact'      => ConfigServer::get('shop', 'contact' ),//联系人
            'mobile'       => ConfigServer::get('shop', 'mobile'),//联系手机号
            'province_id'  => ConfigServer::get('shop', 'province_id'),//省份id
            'city_id'      => ConfigServer::get('shop', 'city_id'),//市id
            'district_id'  => ConfigServer::get('shop', 'district_id'),//区id
            'address'      => ConfigServer::get('shop', 'address'),//详细地址
        ];
        $this->assign([
            'config'  => $config,
        ]);
        return $this->fetch();
    }
}
